﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;



namespace WindowsGame
{
    class Rendering : Microsoft.Xna.Framework.DrawableGameComponent
    {
        SpriteBatch spriteBatch;
        TerrainRenderer terrainRenderer;
		ObjectRenderer objectRenderer;
		EnemiesRenderer enemiesRenderer;
		BulletsRenderer bulletsRenderer;
        HeadUpDisplayRenderer headUpDisplayRenderer;

        IGameLogicService gameLogicService;
        IGameResourcesService gameResourcesService;



        public Rendering(Game game)
            : base(game)
        {
            // Add services here
        }



        public override void Initialize()
        {
			gameLogicService = Game.Services.GetService(typeof(IGameLogicService)) as IGameLogicService;
            gameResourcesService = Game.Services.GetService(typeof(IGameResourcesService)) as IGameResourcesService;

            base.Initialize();
        }



        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);

			terrainRenderer = new TerrainRenderer(GraphicsDevice, gameResourcesService.Terrain, gameLogicService);
            terrainRenderer.LoadContent();

			objectRenderer = new ObjectRenderer(GraphicsDevice, gameResourcesService.Object, gameLogicService);
			enemiesRenderer = new EnemiesRenderer(GraphicsDevice, gameResourcesService.Enemies, gameLogicService);
			bulletsRenderer = new BulletsRenderer(GraphicsDevice, gameResourcesService.Bullet, gameLogicService);

			headUpDisplayRenderer = new HeadUpDisplayRenderer(spriteBatch, gameResourcesService.HeadUpDisplay,
				gameLogicService.HeadUpDisplayLogic);

            base.LoadContent();
        }



        protected override void Dispose(bool disposing)
        {
            // Remove services here
            base.Dispose(disposing);
        }



        public override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);

            GraphicsDevice.BlendState = BlendState.Opaque; // These because spriteBatch changes device states
            GraphicsDevice.DepthStencilState = DepthStencilState.Default;

			terrainRenderer.Draw(gameTime);
			objectRenderer.Draw(gameTime);
			enemiesRenderer.Draw(gameTime);
			bulletsRenderer.Draw(gameTime);

            headUpDisplayRenderer.Draw(gameTime);

            base.Draw(gameTime);
        }
    }
}
